package com.buba.record.controller;


import com.buba.pojo.record.WaitingRecord;
import com.buba.pojoVo.DivisionTypeVo;
import com.buba.record.service.impl.WaitingRecordServiceImpl;
import com.buba.utils.R;
import com.buba.utils.RedisUtil;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 候诊记录表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Slf4j
@RestController
@RequestMapping("/record/waiting-record")
public class WaitingRecordController {


    private static Integer Serialnnumber = 1;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<String , String> redisTemplate;


    @Autowired
    private WaitingRecordServiceImpl waitingRecordService;


    /*
     * @Author: 张庆威
     * @Description: (确认预约信息 生成候诊记录)
     * @Date: 19:07 2022/6/15
     * @Param: [patientId, DepartmentId, doctorId, time]
     * @return: com.buba.utils.R
     **/
    @GetMapping({"/addhozhen/{patientId}/{DepartmentId}/{doctorId}/{time}"})
    public R addhozhen(@PathVariable("patientId") Integer patientId,@PathVariable("DepartmentId") Integer DepartmentId,@PathVariable("doctorId") Integer doctorId,@PathVariable("time") String time) {
        log.info("patientId==="+patientId);
        log.info("DepartmentId==="+DepartmentId);
        log.info("doctorId==="+doctorId);
        WaitingRecord waitingRecord = new WaitingRecord();
        waitingRecord.setOfPatientId(patientId);
        waitingRecord.setAdministrativeId(DepartmentId);
        waitingRecord.setDoctorId(doctorId);
        waitingRecord.setSerial(Serialnnumber++);
        waitingRecord.setUpdateTime(new Date());
        waitingRecord.setCreateTime(new Date());
        waitingRecord.setDeleteFlag("0");
        waitingRecord.setHouzhenTime(time);
        boolean save = waitingRecordService.save(waitingRecord);
        log.info("save=="+save);
        if(save == true){

            int id = waitingRecord.getId();
            log.info("id===="+id);
            return R.ok().data("id",id);
        }else{
            return R.error();
        }
    }


    /*
     * @Author: 张庆威
     * @Description: (通过id 获取就诊人序号)
     * @Date: 19:23 2022/6/19
     * @Param: [userPhone]
     * @return: com.buba.utils.R
     **/
    @GetMapping("/getxuhao/{id}")
    public R getxuhao(@PathVariable("id") Integer id){
        log.info("id:::::::"+id);

        WaitingRecord waitingRecord=waitingRecordService.getxuhao(id);

        if(waitingRecord != null){
            return R.ok().data("waitingRecord",waitingRecord);
        }else{
            return R.error();
        }
    }


    /*
     * @Author: 张庆威
     * @Description: (从redis中获取当前就诊人所在的位置)
     * @Date: 19:39 2022/6/19
     * @Param: [id]
     * @return: com.buba.utils.R
     **/
    @GetMapping("/getRedis/{id}")
    public R getRedis(@PathVariable("id") String id){
        log.info("id:::::::"+id);
        Integer a = null;
        List<Object> queue = redisUtil.lGet("queue", 0, -1);
        System.out.println(queue.size());
        for (int i = 0; i < queue.size() ; i++) {
            System.out.println(queue.get(i));
            if (id.equals(queue.get(i))){
                a = i;
            }
        }
        log.info("a===="+a);
        if(a != null){
            return R.ok().data("a",a);
        }else{
            return R.error();
        }
    }

}

